package edu.umassd.hciproject;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MyPlacesActivity extends FragmentActivity implements
		EditPlaceDialogFragment.OnSaveListener, View.OnClickListener,
		AdapterView.OnItemClickListener, DialogInterface.OnClickListener {

	Button bAdd;
	ListView listView;
	ArrayAdapter<String> listAdapter;
	MyStorageManager manager;
	String selection;
	public static final String[] options = {"Edit","Delete"};

	public static final String MY_PLACES_FILENAME = "my_places.json";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_listing);

		manager = new MyStorageManager(this, MY_PLACES_FILENAME);
		listView = (ListView) findViewById(R.id.lvCommonListing);
		listAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1);
		fillList();
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(this);

		bAdd = (Button) findViewById(R.id.bAddItem);
		bAdd.setOnClickListener(this);
	}

	private void fillList() {
		// TODO Auto-generated method stub
		ArrayList<String> names = manager.getNames();
		listAdapter.clear();
		for (int i = 0; i < names.size(); i++)
			listAdapter.add(names.get(i));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_my_places, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		EditPlaceDialogFragment dialog = new EditPlaceDialogFragment();
		dialog.show(getSupportFragmentManager(), "NewPlaceDialog");
	}

	@Override
	public void onClick(DialogFragment dialog, String name, String address) {
		// TODO Auto-generated method stub
		String message = null;
		if (name.length() == 0) {
			message = "No name entered";
		} else {
			if (listAdapter.getPosition(name) >= 0) {
				manager.updateElement(name, address);
				message = "Place updated";
			} else {
				if (manager.putElement(name, address)) {
					listAdapter.add(name);
					listAdapter.notifyDataSetChanged();
					message = "Saved";
				} else {
					message = "Unable to save";
				}
			}
			dialog.dismiss();
		}
		Toast t = Toast.makeText(this, message, Toast.LENGTH_SHORT);
		t.show();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int pos,
			long id) {
		selection = listAdapter.getItem(pos);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this).setItems(options, this);
		builder.show();
	}

	@Override
	public void onClick(DialogInterface dialog, int item) {
		// TODO Auto-generated method stub
		if (options[item].contentEquals("Edit")) {
			EditPlaceDialogFragment editDialog = new EditPlaceDialogFragment();
			editDialog.show(getSupportFragmentManager(), "EditPlaceDialog");
			editDialog.setPlaceName(selection);
			editDialog.setPlaceAddress((String) manager.getContentsOf(selection));
		} else if (options[item].contentEquals("Delete")) {
			manager.removeElement(selection);
			listAdapter.remove(selection);
			listAdapter.notifyDataSetChanged();
		} 
	}
}
